package com.zh.chat.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import com.zh.chat.controller.ServerController;
import com.zh.chat.entity.UDPProtocol;
import com.zh.chat.entity.User;
import com.zh.chat.entity.UserStatus;

//存放在线用户实例的容器
/**
 * 
 */
public class OnlineUserManager {
	private static Map<Long, User> onlineUser = new HashMap<Long, User>();
	/**
	 * 存放检查用户状态定时器的容器
	 */
	private static Map<Long, TimerTask> checkOnlineMap = new HashMap<Long, TimerTask>();
	private static Timer timer = new Timer();
	
	public static  User getUser(Long id) {
		return onlineUser.get(id);
	}
	
	public static void addUser(User user){
		onlineUser.put(user.getSysId(),user);
	}
	
	public static User removeUser(Long id){
		return onlineUser.remove(id);
	}
	/**
	 * 
	 * @param id
	 * {@link User}的Id
	 * @param status
	 * {@link UserStatus}类型的整型
	 * @return
	 */
	public static User changeStatus(Long id,int status){
		User user = onlineUser.get(id);
		if(status==UserStatus.OUTLINE){
			onlineUser.remove(status);
			user.setStatus(status);
			return user;
		}else{
			user.setStatus(status);
			onlineUser.put(id, user);
			return user;
		}
	}
	
	/**
	 * 
	 * @param id
	 * {@link User}的Id
	 * @param netDTO
	 * {@link NetDTO}实例
	 * @param TIME_OUT
	 * 延时秒(单位ms)
	 */
	public static void startCheckStatus(Long id,int TIME_OUT) {
		TimerTask cktt = new CheckTask(id);
		timer.schedule(cktt, 7000, TIME_OUT);// 延时6秒后给用户发送确认是否在线的信息并且每隔15秒发送一次
		checkOnlineMap.put(id, cktt);
	}
	
	/**
	 * 
	 * @param id
	 * {@link User}的Id
	 * @param status
	 * {@link UserStatus}类型的整型
	 */
	public static void setStatus(Long id, int status) {
		if (status != UserStatus.ONLINE) {
			User user = onlineUser.get(id);
			user.setStatus(status);
			OnlineUserManager.addUser(user);
		} else {
			OnlineUserManager.removeUser(id);
		}
	}
}
class CheckTask extends TimerTask {
	/**
	 * 检查在线用户的进程集合<br>
	 * Integer 用户的ID<br>
	 * Thread 检查用户是否在线的定时器线程
	 */
	private User user = null;
	private Long checkId = 0L;
	UDPProtocol protocol = null;

	public CheckTask(Long id) {
		user = OnlineUserManager.getUser(id);
		checkId = id;
	}

	public void run() {
		//TODO 输出当前正在检查的用户对象
		System.out.println("ID:"+checkId + "用户,当前的状态是" + user.getStatus() + "  "
				+ new java.util.Date(System.currentTimeMillis()));
			if (user.getStatus()!=UserStatus.CHECKING) {
				protocol = new UDPProtocol();
				protocol.setClientPort(user.getClientPort());
				protocol.setClientIp(user.getClientIp());
				ServerController.getInstance().getNetService().requestCheck(protocol);
				
				user.setStatus(UserStatus.CHECKING);// 设置当前用户正在处于检查状态
				System.out.println("ID: " + checkId + " 正在检查…");
			} else if (user.getStatus() == UserStatus.CHECKING) {
				OnlineUserManager.removeUser(checkId);
				System.out.println("用户" + checkId + "已经掉线");
				this.cancel();
			}
	}

}
